import '@/styles/main.less'
import 'vue-photo-preview/dist/skin.css'
import '@/api'
import '@/utils/axios'
import '@/filter'
import Vue from 'vue'
import App from '@/App'
import router from '@/router'
import store from '@/store'
import '@/utils/rem'
import '../node_modules/mint-ui/lib/style.min.css'
import FastClick from 'fastclick'
import axiosHelper from './utils/axiosConfig'
import { code, getTokens, getToken } from '@/config/getToken'
import Bridge from './utils/bridge.js'
import Vant from 'vant'
import 'vant/lib/index.css'
import { Toast } from 'vant'
import gaoDeMap from '@/components/gaoDeMap'
import { getQueryString } from '@/utils/utils'
import '@/plugins/vant.js'
import LiquorTree from 'liquor-tree'
import BaiduMap from 'vue-baidu-map'
Vue.component('gaoDeMap', gaoDeMap)

let toast = function (msg, time = 2000) {
    return Toast({
        message: msg,
        // position: 'bottom',
        duration: time
    })
}

Vue.toast = toast
Vue.prototype.$toast = toast
Vue.prototype.$bridge = Bridge
import preview from 'vue-photo-preview'
Vue.config.productionTip = false
Vue.use(axiosHelper, { ip: '' })
Vue.use(preview, { fullscreenEl: false })
Vue.use(Vant)
Vue.use(BaiduMap, { ak: 'bOU3qzjaY5mfkbAnrJs9LNvRtxoRRrVe' })
Vue.use(LiquorTree)
// Fix: avoid "Unable to preventDefault inside passive event listener" errors
;(function () {
    let supportsPassive = false
    try {
        const opts = Object.defineProperty({}, 'passive', {
            get() {
                supportsPassive = true
            }
        })
        window.addEventListener('testPassive', null, opts)
        window.removeEventListener('testPassive', null, opts)
    } catch (e) {
        // ignore
    }

    if (supportsPassive && typeof EventTarget !== 'undefined') {
        const _addEventListener = EventTarget.prototype.addEventListener
        EventTarget.prototype.addEventListener = function (type, listener, options) {
            // Force non-passive for touch and wheel events when caller didn't explicitly set passive
            if (type === 'touchstart' || type === 'touchmove' || type === 'touchend' || type === 'wheel') {
                // if boolean (capture) or undefined, convert to options object
                if (options === undefined || typeof options === 'boolean') {
                    options = { passive: false, capture: !!options }
                } else if (options && typeof options === 'object' && options.passive === undefined) {
                    options = Object.assign({}, options, { passive: false })
                }
            }
            return _addEventListener.call(this, type, listener, options)
        }
    }
})()
FastClick.attach(document.body) // 解决移动端300ms延迟

function initLocal() {
    const token = getQueryString('token') || ''
    const tenantId = getQueryString('tenantId') || ''
    const projectId = getQueryString('projectId') || ''
    const projectName = getQueryString('projectName') || ''
    const mobile = getQueryString('mobile') || ''

    if (token) {
        localStorage.token = token
    }
    if (tenantId) {
        localStorage.tenantId = tenantId
    }
    if (projectId) {
        localStorage.projectId = projectId
    }
    if (projectName) {
        localStorage.projectName = projectName
    }
    if (mobile) {
        localStorage.mobile = mobile
    }
}

initLocal()

let app = null
;(async () => {
    await import('./utils/axios')
    new Vue({
        el: '#app',
        router,
        store,
        render: (h) => h(App)
    })

})()
// 初始化实例
// if (process.env.NODE_ENV === 'development') {
//     ;(async () => {
//         await import('./utils/axios')
//         // await getToken()
//         new Vue({
//             el: '#app',
//             router,
//             store,
//             render: (h) => h(App)
//         })
//     })()
// } else if (window.location.href.includes('from') || window.sessionStorage.from === 'mobile') {
//     ;(async () => {
//         window.sessionStorage.from = 'mobile'
//         console.log('window.location.href', window.location.href)
//         // 合同详情是从审批那边通过h5打开的，通过 token 来登录，直接实例化，避免走到真机环境的判断里
//         await import('./utils/axios')
//         // await getToken()
//         new Vue({
//             el: '#app',
//             router,
//             store,
//             render: (h) => h(App)
//         })
//     })()
// } else {
//     try {
//         if (navigator.userAgent.indexOf('Html5Plus') > 0) {
//             // 如果是真机环境
//             // 初始化实例
//             document.addEventListener('plusready', async function () {
//                 await import('./utils/axios')
//                 // await getToken()
//                 new Vue({
//                     el: '#app',
//                     router,
//                     store,
//                     render: (h) => h(App)
//                 })
//             })
//         } else {
//             throw new Error('你正在使用浏览器环境')
//         }
//     } catch (err) {
//         // 这里主要是为了线上直接访问h5页面
//         ;(async () => {
//             await import('./utils/axios')
//             // await getToken()
//             new Vue({
//                 el: '#app',
//                 router,
//                 store,
//                 render: (h) => h(App)
//             })
//         })()
//     }
// }

// 保存到相册的结果
window.addEventListener('message', function (e) {
    // console.log('子页面的window.addEventListener', e.data)
    if (e.data && e.data.handle == 'saveImgToGalleryResult') {
        let result = e.data.result
        app.$bus.$emit('saveImgToGalleryResult', result)
    }
})
